
import RPi.GPIO as GPIO
import time

ledPinA = 12;
switchPinA = 11;
switchStateA = 0;
ledPinB = 16;
switchPinB = 15;
switchStateB = 0;

brightness   = 0;

def makerobo_setup():
    global pwmctlLedA
    global pwmctlLedB
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    GPIO.setup(ledPinA,GPIO.OUT)
    GPIO.setup(ledPinB,GPIO.OUT)
    GPIO.setup(switchPinA,GPIO.IN)
    GPIO.setup(switchPinB,GPIO.IN)
    GPIO.output(ledPinA, GPIO.HIGH)
    GPIO.output(ledPinB, GPIO.HIGH)

    pwmctlLedA = GPIO.PWM(ledPinA, 5000)
    pwmctlLedA.start(0)

    pwmctlLedB = GPIO.PWM(ledPinB, 5000)
    pwmctlLedB.start(0)
    print("set up ok")

    

# 比例缩放函数
def makerobo_pwm_map(x,in_min,in_max,out_min,out_max):
    return (x-in_min)*(out_max-out_min)/(in_max-in_min) + out_min

def loop():
    global brightness

    while True:
        switchStateA = GPIO.input(switchPinA);
        if (switchStateA == 1 and brightness != 100):
            brightness+=1
            

        switchStateB = GPIO.input(switchPinB);
        if (switchStateB == 1 and brightness != 0):
            brightness-=1
            
        #reta=makerobo_pwm_map(brightness,0,255,0,100)
        #retb=  makerobo_pwm_map(255-brightness,0,255,0,100)

        pwmctlLedA.ChangeDutyCycle(100-brightness)
        pwmctlLedB.ChangeDutyCycle(brightness)
        print("statusA={},statusB={}".format(switchStateA,switchStateB))
        print("bre={},100-bre={}".format(brightness,100-brightness))

        time.sleep(0.01)

if __name__=='__main__':
    makerobo_setup()
    loop()
    while True:
        GPIO.output(ledPinA, GPIO.HIGH)
        GPIO.output(ledPinB, GPIO.HIGH)
        time.sleep(2)
        GPIO.output(ledPinA, GPIO.LOW)
        GPIO.output(ledPinB, GPIO.LOW)
        time.sleep(2)